from logging import log
import pymysql
import readConfig as readConfig
from common.log import MyLog as Log
localreadConfig=readConfig.ReadConfig()
class MyDB:
    global host,username,password,port,database,config
    host =localreadConfig.get_db("host")
    username=localreadConfig.get_db("username")
    password = localreadConfig.get_db("password")
    port= localreadConfig.get_db("port")
    database =localreadConfig.get_db("database")
    config={
        'host':str(host),
        'username':username,
        'password':password,
        'port':int(port),
        'database':database

    }
    def __init__(self):
        self.log=Log.get_log()
        self.logger=self.log.get_logger()
        self.db=None
        self.cursor=None
    def connectDB(self):
        try:
            self.db=pymysql.connect(**config)
            self.cursor =self.db.cursor()
            self.logger.info("connect sucessful!")
        except  ConnectionError as ex:
            self.logger.error(str(ex))
    def executeSQL(self,sql,params):
        self.connectDB()
        self.cursor.execute(sql,params)
        self.db.commit()
        return self.cursor
    def get_one(self,cursor):
        value=self.cursor.fetchone()
        return value
    def get_all(self,cursor):
        value = self.cursor.fetchall()
        return value
    def closeDB(self):
        self.db.close()
        print("Database closed!")